package c.e.a.a.f.p.i;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabaseLockedException;
import android.os.SystemClock;
import android.util.Base64;
import b.i.b.f;
import c.e.a.a.f.e;
import c.e.a.a.f.h;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

/* compiled from: SQLiteEventStore */
public class t implements c, c.e.a.a.f.q.b {
    public static final c.e.a.a.a e = new c.e.a.a.a("proto");
    public final z a;
    public final c.e.a.a.f.r.a b;
    public final c.e.a.a.f.r.a c;
    public final d d;

    /* compiled from: SQLiteEventStore */
    public interface b<T, U> {
        U a(T t);
    }

    /* compiled from: SQLiteEventStore */
    public static class c {
        public final String a;
        public final String b;

        public c(String str, String str2, a aVar) {
            this.a = str;
            this.b = str2;
        }
    }

    /* compiled from: SQLiteEventStore */
    public interface d<T> {
        T a();
    }

    public t(c.e.a.a.f.r.a aVar, c.e.a.a.f.r.a aVar2, d dVar, z zVar) {
        this.a = zVar;
        this.b = aVar;
        this.c = aVar2;
        this.d = dVar;
    }

    public static String s(Iterable<h> iterable) {
        StringBuilder stringBuilder = new StringBuilder("(");
        Iterator it = iterable.iterator();
        while (it.hasNext()) {
            stringBuilder.append(((h) it.next()).b());
            if (it.hasNext()) {
                stringBuilder.append(',');
            }
        }
        stringBuilder.append(')');
        return stringBuilder.toString();
    }

    public static <T> T t(Cursor cursor, b<Cursor, T> bVar) {
        try {
            T a = bVar.a(cursor);
            return a;
        } finally {
            cursor.close();
        }
    }

    public int D() {
        long a = this.b.a() - this.d.b();
        SQLiteDatabase n = n();
        n.beginTransaction();
        try {
            Integer valueOf = Integer.valueOf(n.delete("events", "timestamp_ms < ?", new String[]{String.valueOf(a)}));
            n.setTransactionSuccessful();
            return valueOf.intValue();
        } finally {
            n.endTransaction();
        }
    }

    public void F(Iterable<h> iterable) {
        if (iterable.iterator().hasNext()) {
            StringBuilder g = c.b.a.a.a.g("DELETE FROM events WHERE _id in ");
            g.append(s(iterable));
            n().compileStatement(g.toString()).execute();
        }
    }

    public Iterable<h> L(h hVar) {
        return (Iterable) q(new j(this, hVar));
    }

    public void O(h hVar, long j) {
        q(new i(j, hVar));
    }

    public h T(h hVar, e eVar) {
        f.n0("SQLiteEventStore", "Storing event with priority=%s, name=%s for destination %s", hVar.d(), eVar.g(), hVar.b());
        long longValue = ((Long) q(new s(this, hVar, eVar))).longValue();
        if (longValue < 1) {
            return null;
        }
        return new b(longValue, hVar, eVar);
    }

    public Iterable<h> U() {
        SQLiteDatabase n = n();
        n.beginTransaction();
        try {
            List list = (List) t(n.rawQuery("SELECT distinct t._id, t.backend_name, t.priority, t.extras FROM transport_contexts AS t, events AS e WHERE e.context_id = t._id", new String[0]), p.a);
            n.setTransactionSuccessful();
            return list;
        } finally {
            n.endTransaction();
        }
    }

    public <T> T a(c.e.a.a.f.q.b.a<T> aVar) {
        SQLiteDatabase n = n();
        r(new m(n), n.a);
        try {
            T execute = aVar.execute();
            n.setTransactionSuccessful();
            return execute;
        } finally {
            n.endTransaction();
        }
    }

    public long a0(h hVar) {
        Cursor rawQuery = n().rawQuery("SELECT next_request_ms FROM transport_contexts WHERE backend_name = ? and priority = ?", new String[]{hVar.b(), String.valueOf(c.e.a.a.f.s.a.a(hVar.d()))});
        try {
            Long valueOf;
            if (rawQuery.moveToNext()) {
                valueOf = Long.valueOf(rawQuery.getLong(0));
            } else {
                valueOf = Long.valueOf(0);
            }
            rawQuery.close();
            return valueOf.longValue();
        } catch (Throwable th) {
            rawQuery.close();
        }
    }

    public void close() {
        this.a.close();
    }

    public boolean d0(h hVar) {
        SQLiteDatabase n = n();
        n.beginTransaction();
        try {
            Boolean bool;
            if (o(n, hVar) == null) {
                bool = Boolean.FALSE;
            } else {
                bool = (Boolean) t(n().rawQuery("SELECT 1 FROM events WHERE context_id = ? LIMIT 1", new String[]{r5.toString()}), q.a);
            }
            n.setTransactionSuccessful();
            return bool.booleanValue();
        } finally {
            n.endTransaction();
        }
    }

    public void e0(Iterable<h> iterable) {
        if (iterable.iterator().hasNext()) {
            StringBuilder g = c.b.a.a.a.g("UPDATE events SET num_attempts = num_attempts + 1 WHERE _id in ");
            g.append(s(iterable));
            String stringBuilder = g.toString();
            SQLiteDatabase n = n();
            n.beginTransaction();
            try {
                n.compileStatement(stringBuilder).execute();
                n.compileStatement("DELETE FROM events WHERE num_attempts >= 16").execute();
                n.setTransactionSuccessful();
            } finally {
                n.endTransaction();
            }
        }
    }

    public SQLiteDatabase n() {
        z zVar = this.a;
        zVar.getClass();
        return (SQLiteDatabase) r(new o(zVar), r.a);
    }

    public final Long o(SQLiteDatabase sQLiteDatabase, h hVar) {
        StringBuilder stringBuilder = new StringBuilder("backend_name = ? and priority = ?");
        ArrayList arrayList = new ArrayList(Arrays.asList(new String[]{hVar.b(), String.valueOf(c.e.a.a.f.s.a.a(hVar.d()))}));
        if (hVar.c() != null) {
            stringBuilder.append(" and extras = ?");
            arrayList.add(Base64.encodeToString(hVar.c(), 0));
        }
        SQLiteDatabase sQLiteDatabase2 = sQLiteDatabase;
        Cursor query = sQLiteDatabase2.query("transport_contexts", new String[]{"_id"}, stringBuilder.toString(), (String[]) arrayList.toArray(new String[0]), null, null, null);
        try {
            Long valueOf;
            if (query.moveToNext()) {
                valueOf = Long.valueOf(query.getLong(0));
            } else {
                valueOf = null;
            }
            query.close();
            return valueOf;
        } catch (Throwable th) {
            query.close();
        }
    }

    public final <T> T q(b<SQLiteDatabase, T> bVar) {
        SQLiteDatabase n = n();
        n.beginTransaction();
        try {
            T a = bVar.a(n);
            n.setTransactionSuccessful();
            return a;
        } finally {
            n.endTransaction();
        }
    }

    public final <T> T r(d<T> dVar, b<Throwable, T> bVar) {
        long a = this.c.a();
        while (true) {
            try {
                d dVar2 = dVar2.a();
                return dVar2;
            } catch (SQLiteDatabaseLockedException e) {
                if (this.c.a() >= ((long) this.d.a()) + a) {
                    return bVar.a(e);
                }
                SystemClock.sleep(50);
            }
        }
    }
}
